import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_account_book/pages/home/widgets/drawer.dart';
import 'package:flutter_account_book/routes.dart';
import 'package:flutter_account_book/utils/db/entities/account_book.dart';

import 'logic.dart';
import 'widgets/page.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<StatefulWidget> createState() => _MyHomePage();
}

class _MyHomePage extends State<MyHomePage> {
  late MyHomeLogic logic;
  @override
  void initState() {
    super.initState();
    logic = Get.put(MyHomeLogic());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Obx(() => Text(logic.state.title.value)),
          actions: [
            IconButton(
                onPressed: () {
                  Get.toNamed(Routers.analysisPage,
                      arguments: logic.state.curId.value);
                },
                icon: const Icon(Icons.pie_chart))
          ],
        ),
        drawer: HomeDrawer(onSettings: () {
          Get.toNamed(Routers.settingPage);
        }, onSelected: (AccountBook accountBook) {
          logic.state.curId.value = accountBook.id;
          logic.state.title.value = accountBook.name;
        }),
        body: Obx(
          () => AccountChildPage(curId: logic.state.curId.value),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            Get.toNamed(Routers.addRecordPage,
                    arguments: logic.state.curId.value)
                ?.then((_) {
              logic.reload();
            });
          },
          shape: const CircleBorder(),
          child: const Icon(Icons.add),
        ));
  }
}
